﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Permissions;
using System.Windows.Forms.Design;
using NetJsWire.JsonRpcHandler;

namespace NetJsWire.Widgets.Config
{
    [PermissionSet( SecurityAction.Demand, Name = "FullTrust" )]
    [ReflectionPermissionAttribute( SecurityAction.Demand, Unrestricted = true )]
    public class ConfigOptionsPropertyTab : PropertyTab
    {
        private string img = "AAEAAAD/////AQAAAAAAAAAMAgAAAFRTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj0xLjAuMzMwMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAABVTeXN0ZW0uRHJhd2luZy5CaXRtYXABAAAABERhdGEHAgIAAAAJAwAAAA8DAAAA9gAAAAJCTfYAAAAAAAAANgAAACgAAAAIAAAACAAAAAEAGAAAAAAAAAAAAMQOAADEDgAAAAAAAAAAAAD///////////////////////////////////9ZgABZgADzPz/zPz/zPz9AgP//////////gAD/gAD/AAD/AAD/AACKyub///////+AAACAAAAAAP8AAP8AAP9AgP////////9ZgABZgABz13hz13hz13hAgP//////////gAD/gACA/wCA/wCA/wAA//////////+AAACAAAAAAP8AAP8AAP9AgP////////////////////////////////////8L";

        #region Overrides of PropertyTab

        /// <summary>
        ///   Obtiene el nombre de la ficha de propiedades.
        /// </summary>
        /// <returns>
        ///   Nombre de la ficha de propiedades.
        /// </returns>
        public override string TabName
        {
            get { return "Config Options"; }
        }

        /// <summary>
        ///   Obtiene las propiedades del componente especificado que coinciden con los atributos especificados.
        /// </summary>
        /// <returns>
        ///   <see cref = "T:System.ComponentModel.PropertyDescriptorCollection" /> que contiene las propiedades.
        /// </returns>
        /// <param name = "component">Componente cuyas propiedades se van a recuperar. </param>
        /// <param name = "attributes">Matriz de tipo <see cref = "T:System.Attribute" /> que indica los atributos de las propiedades que se van a recuperar. </param>
        public override PropertyDescriptorCollection GetProperties( object component, Attribute[] attributes )
        {
            if ( component is IWidget )
            {
                return TypeDescriptor.GetProperties( ( ( IWidget ) component ).ConfigOptions, new Attribute[] { new BrowsableAttribute( true ) } );
            }

            if ( attributes != null )
                return TypeDescriptor.GetProperties( component, attributes );

            return TypeDescriptor.GetProperties( component );
        }

        public override PropertyDescriptorCollection GetProperties( object component )
        {
            if ( component is IWidget )
                return GetProperties( component, null );

            return TypeDescriptor.GetProperties( component ); ;
        }

        public override PropertyDescriptorCollection GetProperties( ITypeDescriptorContext context, object component, Attribute[] attributes )
        {
            if ( component is IWidget )
            {
                return TypeDescriptor.GetProperties( ( ( IWidget ) component ).ConfigOptions, new Attribute[] { new BrowsableAttribute( true ) } );
            }
            if ( attributes != null )
                return TypeDescriptor.GetProperties( component, attributes );

            return TypeDescriptor.GetProperties( component );
        }

        #endregion

        // Provides an image for the property tab.
        public override Bitmap Bitmap
        {
            get
            {
                var bmp = new Bitmap( DeserializeFromBase64Text( img ) );
                return bmp;
            }
        }

        // This method can be used to retrieve an Image from a block of Base64-encoded text.
        private static Image DeserializeFromBase64Text( string text )
        {
            Image img = null;
            var memBytes = Convert.FromBase64String( text );
            IFormatter formatter = new BinaryFormatter();
            var stream = new MemoryStream( memBytes );
            img = ( Image ) formatter.Deserialize( stream );
            stream.Close();
            return img;
        }
    }
}
